﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Handy_WPF.MVVM
{
    /// <summary>
    /// 带有惊叹号的提示图形
    /// </summary>
    public class NotifyAdorner : Adorner
    {
        //Visual对象有序集合
        private VisualCollection _visuals;
        //绘制区域
        private Canvas _canvas;
        //图像控件
        private Image _image;
        //轻型控件，用于显示少量流内容
        private TextBlock _toolTip;

        public NotifyAdorner(UIElement adornedElement, string errorMessage) : base(adornedElement)
        {
            _visuals = new VisualCollection(this);

            _image = new Image()
            {
                Width = 16,
                Height = 16,
                Source = new BitmapImage(new Uri("/Resources/warning.png", UriKind.RelativeOrAbsolute))
            };

            _toolTip = new TextBlock() { Text = errorMessage };
            _image.ToolTip = _toolTip;

            _canvas = new Canvas();
            _canvas.Children.Add(_image);
            _visuals.Add(_canvas);
        }

        //获取此元素内可视子元素的数目
        protected override int VisualChildrenCount => _visuals.Count;

        //重写从子元素集合中返回指定索引处的子元素
        protected override Visual GetVisualChild(int index)
        {
            return _visuals[index];
        }

        public void ChangeToolTip(string errorMessage)
        {
            _toolTip.Text = errorMessage;
        }

        //实现装饰器的任何自定义度量行为
        protected override Size MeasureOverride(Size constraint)
        {
            return base.MeasureOverride(constraint);
        }

        //为 FrameworkElement 派生类定位子元素并确定大小
        protected override Size ArrangeOverride(Size finalSize)
        {
            _canvas.Arrange(new Rect(finalSize));
            _image.Margin = new Thickness(finalSize.Width + 3, 0, 0, 0);

            return base.ArrangeOverride(finalSize);
        }
    }
}
